//
// Created by wujehy on 2020/5/31.
//

#include <iostream>
#include <chrono>
#include "Tool/KeyValueBase.h"
using namespace GeeJoan::DataBase;
// 获取时间戳穿, 微秒级别
int64_t timeMicro() {
    std::chrono::time_point< std::chrono::system_clock> p =  std::chrono::system_clock::now();
    return  std::chrono::duration_cast< std::chrono::microseconds>(p.time_since_epoch()).count();
}
/**
 * @brief　测试中100K 的 KV 的耗时
 * @return
 */
int main()
{
    std::string fileName= "test100k.gkv";

    KeyValueFileService serviceSave;

    serviceSave.setMFileName(fileName);
    int64_t insertStartTime =  timeMicro();
    for (int i = 0; i < 100000; ++i)
    {
        serviceSave.Set2("key"+std::to_string(i) , "value"+std::to_string(i));
    }
    int64_t insertEndTime =  timeMicro();

    std::cout <<" use time insert : "<< insertEndTime - insertStartTime <<" μs " <<std::endl;

    int64_t saveStartTime =  timeMicro();
    serviceSave.save();
    int64_t saveEndTime =  timeMicro();

    std::cout <<" use time Save : "<< saveEndTime - saveStartTime <<" μs " <<std::endl;

    // 读取
    KeyValueFileService serviceLoad;

    serviceLoad.setMFileName(fileName);
    int64_t loadStartTime =  timeMicro();
    serviceLoad.load();
    int64_t loadEndTime =  timeMicro();
    std::cout <<" use time Load : "<< loadEndTime - loadStartTime <<" μs " <<std::endl;

    // 测试 读取 第页 20000 的数据
    int64_t getStartTime =  timeMicro();

    std::string getValue = serviceLoad.Get2("key20000")->getValue(true) ;
    int64_t getEndTime =  timeMicro();

    std::cout <<" test get time : " << getEndTime - getStartTime <<" μs ,  Key20000 : " <<getValue <<std::endl;

    return 0;

}